package com.teacher.game.framework.util;

import android.util.Log;
import com.teacher.game.state.DifficultySelectState;

/**
 * 难度平衡优化工具
 * 
 * 用于分析和优化游戏难度参数，确保三种难度模式提供合适的挑战曲线。
 * 基于游戏设计理论和数据分析，提供科学的难度平衡建议。
 * 
 * 优化方向：
 * - 反应时间要求的渐进性
 * - 学习曲线的合理性
 * - 挫败感和成就感的平衡
 * - 不同技能水平玩家的包容性
 * 
 * @version 1.0
 * @since 2025-09-12
 */
public class DifficultyBalanceOptimizer {
    
    private static final String TAG = "DifficultyBalance";
    
    // 基础参数
    private static final int BASE_SPEED = -200;           // 基础速度
    private static final int BASE_SPACING = 200;          // 基础间距
    private static final float BASE_POWERUP_CHANCE = 0.3f; // 基础道具概率
    
    /**
     * 难度配置数据类
     */\n    public static class DifficultyConfig {\n        public int speed;              // 障碍物速度\n        public int spacing;            // 障碍物间距\n        public float powerUpChance;    // 道具生成概率\n        public float reactionTime;     // 要求反应时间（秒）\n        public int challengeRating;    // 挑战评级（1-10）\n        public String description;     // 难度描述\n        \n        public DifficultyConfig(int speed, int spacing, float powerUpChance, \n                               float reactionTime, int challengeRating, String description) {\n            this.speed = speed;\n            this.spacing = spacing;\n            this.powerUpChance = powerUpChance;\n            this.reactionTime = reactionTime;\n            this.challengeRating = challengeRating;\n            this.description = description;\n        }\n        \n        @Override\n        public String toString() {\n            return String.format(\"速度:%d, 间距:%d, 道具:%.2f, 反应:%.2fs, 挑战:%d - %s\",\n                    speed, spacing, powerUpChance, reactionTime, challengeRating, description);\n        }\n    }\n    \n    /**\n     * 获取当前的难度配置\n     */\n    public static DifficultyConfig getCurrentConfig(int difficulty) {\n        switch (difficulty) {\n            case DifficultySelectState.DIFFICULTY_EASY:\n                return new DifficultyConfig(\n                    (int)(BASE_SPEED * 0.7f),    // -140 (慢30%)\n                    250,                          // 大间距\n                    BASE_POWERUP_CHANCE * 1.5f,  // 0.45 (多50%道具)\n                    1.8f,                         // 1.8秒反应时间\n                    3,                            // 挑战评级3/10\n                    \"适合新手，宽松的反应时间\"\n                );\n                \n            case DifficultySelectState.DIFFICULTY_NORMAL:\n                return new DifficultyConfig(\n                    BASE_SPEED,                   // -200 (标准)\n                    200,                          // 标准间距\n                    BASE_POWERUP_CHANCE,          // 0.30 (标准道具)\n                    1.0f,                         // 1.0秒反应时间\n                    5,                            // 挑战评级5/10\n                    \"平衡体验，适度挑战\"\n                );\n                \n            case DifficultySelectState.DIFFICULTY_HARD:\n                return new DifficultyConfig(\n                    (int)(BASE_SPEED * 1.3f),    // -260 (快30%)\n                    160,                          // 小间距\n                    BASE_POWERUP_CHANCE * 0.5f,  // 0.15 (少50%道具)\n                    0.6f,                         // 0.6秒反应时间\n                    8,                            // 挑战评级8/10\n                    \"高手挑战，快速反应需求\"\n                );\n                \n            default:\n                return getCurrentConfig(DifficultySelectState.DIFFICULTY_NORMAL);\n        }\n    }\n    \n    /**\n     * 获取优化建议的难度配置\n     */\n    public static DifficultyConfig getOptimizedConfig(int difficulty) {\n        switch (difficulty) {\n            case DifficultySelectState.DIFFICULTY_EASY:\n                // 优化：更加适合新手\n                return new DifficultyConfig(\n                    -120,                         // 更慢：-120 (慢40%)\n                    300,                          // 更大间距\n                    0.5f,                         // 更多道具\n                    2.2f,                         // 更长反应时间\n                    2,                            // 更低挑战评级\n                    \"新手友好，充分练习时间\"\n                );\n                \n            case DifficultySelectState.DIFFICULTY_NORMAL:\n                // 优化：保持平衡，微调参数\n                return new DifficultyConfig(\n                    -180,                         // 稍微慢一点：-180 (慢10%)\n                    220,                          // 稍大间距\n                    0.35f,                        // 稍多道具\n                    1.1f,                         // 稍长反应时间\n                    5,                            // 保持中等挑战\n                    \"优化平衡，流畅体验\"\n                );\n                \n            case DifficultySelectState.DIFFICULTY_HARD:\n                // 优化：高挑战但不极端\n                return new DifficultyConfig(\n                    -240,                         // 快但不极端：-240 (快20%)\n                    180,                          // 中等偏小间距\n                    0.2f,                         // 少但不稀缺的道具\n                    0.75f,                        // 要求快反应但可达成\n                    7,                            // 高挑战但不极端\n                    \"挑战进阶，考验技巧\"\n                );\n                \n            default:\n                return getOptimizedConfig(DifficultySelectState.DIFFICULTY_NORMAL);\n        }\n    }\n    \n    /**\n     * 分析难度配置的合理性\n     */\n    public static BalanceAnalysis analyzeDifficultyBalance() {\n        Log.i(TAG, \"========== 难度平衡分析 ==========\");\n        \n        DifficultyConfig easy = getCurrentConfig(DifficultySelectState.DIFFICULTY_EASY);\n        DifficultyConfig normal = getCurrentConfig(DifficultySelectState.DIFFICULTY_NORMAL);\n        DifficultyConfig hard = getCurrentConfig(DifficultySelectState.DIFFICULTY_HARD);\n        \n        BalanceAnalysis analysis = new BalanceAnalysis();\n        \n        // 分析速度递进\n        boolean speedProgression = Math.abs(easy.speed) < Math.abs(normal.speed) && \n                                   Math.abs(normal.speed) < Math.abs(hard.speed);\n        analysis.speedProgressionGood = speedProgression;\n        \n        // 分析间距递减\n        boolean spacingProgression = easy.spacing > normal.spacing && \n                                     normal.spacing > hard.spacing;\n        analysis.spacingProgressionGood = spacingProgression;\n        \n        // 分析道具递减\n        boolean powerUpProgression = easy.powerUpChance > normal.powerUpChance && \n                                    normal.powerUpChance > hard.powerUpChance;\n        analysis.powerUpProgressionGood = powerUpProgression;\n        \n        // 分析反应时间要求\n        boolean reactionTimeProgression = easy.reactionTime > normal.reactionTime && \n                                         normal.reactionTime > hard.reactionTime;\n        analysis.reactionTimeProgressionGood = reactionTimeProgression;\n        \n        // 分析难度跨度\n        float speedRatio = Math.abs(hard.speed) / (float)Math.abs(easy.speed);\n        float spacingRatio = (float)easy.spacing / hard.spacing;\n        float powerUpRatio = easy.powerUpChance / hard.powerUpChance;\n        \n        analysis.speedRatio = speedRatio;\n        analysis.spacingRatio = spacingRatio;\n        analysis.powerUpRatio = powerUpRatio;\n        \n        // 理想比率：1.5-2.5之间\n        analysis.speedRatioGood = (speedRatio >= 1.5f && speedRatio <= 2.5f);\n        analysis.spacingRatioGood = (spacingRatio >= 1.5f && spacingRatio <= 2.0f);\n        analysis.powerUpRatioGood = (powerUpRatio >= 2.0f && powerUpRatio <= 4.0f);\n        \n        // 计算总体平衡分数\n        int score = 0;\n        if (analysis.speedProgressionGood) score += 15;\n        if (analysis.spacingProgressionGood) score += 15;\n        if (analysis.powerUpProgressionGood) score += 15;\n        if (analysis.reactionTimeProgressionGood) score += 15;\n        if (analysis.speedRatioGood) score += 10;\n        if (analysis.spacingRatioGood) score += 10;\n        if (analysis.powerUpRatioGood) score += 10;\n        \n        // 额外奖励：完整性\n        if (score >= 70) score += 10;\n        \n        analysis.overallScore = score;\n        \n        // 输出分析结果\n        Log.i(TAG, \"当前配置:\");\n        Log.i(TAG, \"  简单: \" + easy.toString());\n        Log.i(TAG, \"  一般: \" + normal.toString());\n        Log.i(TAG, \"  困难: \" + hard.toString());\n        Log.i(TAG, \"\");\n        \n        Log.i(TAG, \"递进性分析:\");\n        Log.i(TAG, \"  速度递进: \" + (speedProgression ? \"✓\" : \"✗\"));\n        Log.i(TAG, \"  间距递减: \" + (spacingProgression ? \"✓\" : \"✗\"));\n        Log.i(TAG, \"  道具递减: \" + (powerUpProgression ? \"✓\" : \"✗\"));\n        Log.i(TAG, \"  反应时间递减: \" + (reactionTimeProgression ? \"✓\" : \"✗\"));\n        Log.i(TAG, \"\");\n        \n        Log.i(TAG, \"比率分析:\");\n        Log.i(TAG, \"  速度比率: \" + String.format(\"%.2f\", speedRatio) + (analysis.speedRatioGood ? \" ✓\" : \" (建议1.5-2.5)\"));\n        Log.i(TAG, \"  间距比率: \" + String.format(\"%.2f\", spacingRatio) + (analysis.spacingRatioGood ? \" ✓\" : \" (建议1.5-2.0)\"));\n        Log.i(TAG, \"  道具比率: \" + String.format(\"%.2f\", powerUpRatio) + (analysis.powerUpRatioGood ? \" ✓\" : \" (建议2.0-4.0)\"));\n        Log.i(TAG, \"\");\n        \n        Log.i(TAG, \"总体平衡分数: \" + score + \"/100\");\n        if (score >= 85) {\n            Log.i(TAG, \"评级: 优秀 ⭐⭐⭐\");\n        } else if (score >= 70) {\n            Log.i(TAG, \"评级: 良好 ⭐⭐\");\n        } else if (score >= 60) {\n            Log.i(TAG, \"评级: 及格 ⭐\");\n        } else {\n            Log.i(TAG, \"评级: 需要改进\");\n        }\n        \n        Log.i(TAG, \"===============================\");\n        \n        return analysis;\n    }\n    \n    /**\n     * 提供优化建议\n     */\n    public static void provideOptimizationSuggestions() {\n        Log.i(TAG, \"========== 优化建议 ==========\");\n        \n        BalanceAnalysis analysis = analyzeDifficultyBalance();\n        \n        if (analysis.overallScore >= 85) {\n            Log.i(TAG, \"👍 当前难度平衡良好，无需大幅调整\");\n        } else {\n            Log.i(TAG, \"💡 建议优化项:\");\n            \n            if (!analysis.speedProgressionGood) {\n                Log.i(TAG, \"  - 调整速度递进关系\");\n            }\n            if (!analysis.spacingProgressionGood) {\n                Log.i(TAG, \"  - 调整间距递减关系\");\n            }\n            if (!analysis.powerUpProgressionGood) {\n                Log.i(TAG, \"  - 调整道具频率递减\");\n            }\n            if (!analysis.speedRatioGood) {\n                Log.i(TAG, \"  - 优化速度差异比率到1.5-2.5范围\");\n            }\n            if (!analysis.spacingRatioGood) {\n                Log.i(TAG, \"  - 优化间距差异比率到1.5-2.0范围\");\n            }\n            if (!analysis.powerUpRatioGood) {\n                Log.i(TAG, \"  - 优化道具差异比率到2.0-4.0范围\");\n            }\n        }\n        \n        Log.i(TAG, \"\");\n        Log.i(TAG, \"🎯 优化后的推荐配置:\");\n        DifficultyConfig optEasy = getOptimizedConfig(DifficultySelectState.DIFFICULTY_EASY);\n        DifficultyConfig optNormal = getOptimizedConfig(DifficultySelectState.DIFFICULTY_NORMAL);\n        DifficultyConfig optHard = getOptimizedConfig(DifficultySelectState.DIFFICULTY_HARD);\n        \n        Log.i(TAG, \"  简单: \" + optEasy.toString());\n        Log.i(TAG, \"  一般: \" + optNormal.toString());\n        Log.i(TAG, \"  困难: \" + optHard.toString());\n        \n        Log.i(TAG, \"===============================\");\n    }\n    \n    /**\n     * 计算玩家技能匹配度\n     * @param playerSkillLevel 玩家技能等级(1-10)\n     * @return 推荐的难度\n     */\n    public static int calculateBestDifficultyForSkill(int playerSkillLevel) {\n        if (playerSkillLevel <= 3) {\n            Log.d(TAG, \"技能等级\" + playerSkillLevel + \" -> 推荐简单难度\");\n            return DifficultySelectState.DIFFICULTY_EASY;\n        } else if (playerSkillLevel <= 7) {\n            Log.d(TAG, \"技能等级\" + playerSkillLevel + \" -> 推荐一般难度\");\n            return DifficultySelectState.DIFFICULTY_NORMAL;\n        } else {\n            Log.d(TAG, \"技能等级\" + playerSkillLevel + \" -> 推荐困难难度\");\n            return DifficultySelectState.DIFFICULTY_HARD;\n        }\n    }\n    \n    /**\n     * 平衡分析结果数据类\n     */\n    public static class BalanceAnalysis {\n        public boolean speedProgressionGood = false;\n        public boolean spacingProgressionGood = false;\n        public boolean powerUpProgressionGood = false;\n        public boolean reactionTimeProgressionGood = false;\n        \n        public float speedRatio = 0;\n        public float spacingRatio = 0;\n        public float powerUpRatio = 0;\n        \n        public boolean speedRatioGood = false;\n        public boolean spacingRatioGood = false;\n        public boolean powerUpRatioGood = false;\n        \n        public int overallScore = 0;\n    }\n}